//
//  HorizontalScrollViewController.m
//  ZYOnlineSchool
//
//  Created by 毕武侠 on 2017/1/9.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "HorizontalScrollViewController.h"

@interface HorizontalScrollViewController () <didSelectCellDelegate>

@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation HorizontalScrollViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - get

- (UIView *)lineView {
    if (_lineView == nil) {
        _lineView = [[UIView alloc] init];
        _lineView.size = CGSizeMake(42, 1);
        _lineView.backgroundColor = self.lineColor ? : MainThemeColor;
    }
    return _lineView;
}

- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
    }
    return _scrollView;
}


#pragma mark - init

- (void)setupConfigureView {
    self.view.backgroundColor = [UIColor whiteColor];
    
    if (self.titles.count == 0) {
        return;
    }
    
    if (self.iy == 0) {
        self.iy = 64;
    }
    
    if (self.ih == 0) {
        self.ih = 32;
    }
    
    
    CGFloat cWidth = SCREEN_WIDTH / self.titles.count;
    [self.titles enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        // add btn
        UIButton *btn = [[UIButton alloc] init];
        btn.frame = CGRectMake(idx * cWidth, self.iy, cWidth, self.ih);
        btn.tag = kSelectBtnTag + idx;
        [btn setTitle:obj forState:UIControlStateNormal];
        [btn setTitleColor:self.bNorColor ? : [UIColor blackColor] forState:UIControlStateNormal];
        [btn.titleLabel setFont:self.bFont ? : [UIFont systemFontOfSize:15.0f]];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }];
    
    // add line view
    self.lineView.top = self.iy + self.ih;
    [self.view addSubview:self.lineView];
    
    // add scrollview
    self.scrollView.frame = CGRectMake(0, self.lineView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT - self.lineView.bottom - (self.showTabBar == YES ? 49.0f : 0));
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * self.titles.count, self.scrollView.height);
    [self.view addSubview:self.scrollView];
    
    // add table view
    for (int i = 0; i < self.titles.count; i++) {
        BaseTableView *tableView;
        if (self.showAutoLayout == YES) {
            tableView = [[AutoLayoutTableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, self.scrollView.height)];
        } else {
            tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, self.scrollView.height)];
        }
        tableView.tag = kSelectTableViewTag + i;
        tableView.cellDelegate = self;
        [tableView registerCellClass:self.cellClassName cellIdentifier:self.cellClassNameID];
        tableView.backgroundColor = RGB(242, 243, 244);
        
        //        [tableView reloadNewData];
        [self.scrollView addSubview:tableView];
    }

    CGFloat fx = SCREEN_WIDTH / (self.titles.count * 2) - 21;
    @weakify(self)
    RACSignal *scrollOffSignal = RACObserve(self.scrollView, contentOffset);
    [scrollOffSignal subscribeNext:^(NSValue *value) {
        @strongify(self)
        
        self.lineView.left = [value CGPointValue].x / self.titles.count + fx;
        NSUInteger x = (NSUInteger)[value CGPointValue].x;
        NSUInteger i = x % (NSUInteger)SCREEN_WIDTH;
        if (i == 0) {
            NSUInteger index = x / SCREEN_WIDTH;
            for (int i = 0; i < self.titles.count; i++) {
                UIButton *btn = [self.view viewWithTag:kSelectBtnTag + i];
                if (i == index) {
                    [btn setTitleColor:MainThemeColor forState:UIControlStateNormal];
                } else {
                    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                }
            }
        }
    }];
}

- (void)setTableViewData:(NSMutableArray *)datas AtIndex:(NSUInteger)index {
    BaseTableView *tableView = [self.scrollView viewWithTag:kSelectTableViewTag + index];
    tableView.sourceData = datas;
}

#pragma mark - btn action

- (void)btnAction:(UIButton *)sender {
    NSUInteger index = sender.tag - kSelectBtnTag;
    [self.scrollView setContentOffset:CGPointMake(index *SCREEN_WIDTH, 0) animated:YES];
}

#pragma mark - table delegate

- (void)tableView:(BaseTableView *)tableView didselectIndexPath:(NSIndexPath *)indexPath {
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
